using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameStateMulti : MonoBehaviour {

    public bool player2Active;
    public bool twoShots;
    public bool futureTwoShots;

    public bool player2WasActiveWhenWon;
    public bool gameOver;

    public GameObject gameOverLabel;
    public GameObject penaltyDescriptionLabel;
    public GameObject player1Chevron;
    public GameObject player2Chevron;
    public GameObject player1TwoShots;
    public GameObject player2TwoShots;

    Text gameOverText;
    Text penaltyDescriptionText;
    Image player1ChevronImage;
    Image player2ChevronImage;
    Text player1TwoShotsText;
    Text player2TwoShotsText;

	// Use this for initialization
	void Start ()
    {
        player2Active = false;
        twoShots = false;
        futureTwoShots = false;

        gameOverText = gameOverLabel.GetComponent<Text>();
        gameOverText.text = "";
        penaltyDescriptionText = penaltyDescriptionLabel.GetComponent<Text>();
        penaltyDescriptionText.text = "";

        player1ChevronImage = player1Chevron.GetComponent<Image>();
        player2ChevronImage = player2Chevron.GetComponent<Image>();

        player1TwoShotsText = player1TwoShots.GetComponent<Text>();
        player2TwoShotsText = player2TwoShots.GetComponent<Text>();

        Redraw();
	}

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("menu", LoadSceneMode.Single);
            return;
        }
	}

    public void Potted(BallColour.Colour colour)
    {
        Debug.Log("Ball potted");
        if (colour == GetUnfriendlyColour())
        {
            SetPenaltyText("Potted the opponent's ball. 2 shots.");
            AddPenalty();
        }
        else if (colour == GetFriendlyColour())
        {
            twoShots = true;
        }
        else if (colour == BallColour.Colour.White)
        {
            SetPenaltyText("Potted the cue ball. 2 shots.");
            AddPenalty();
        }
        else if (colour == BallColour.Colour.Black)
        {
            Debug.Log("Black ball potted");
            bool stillRedBalls = false;
            var balls = FindObjectsOfType<BallColour>();
            foreach (BallColour ball in balls)
            {
                if (ball.colour == GetFriendlyColour())
                {
                    stillRedBalls = true;
                    break;
                }
            }
            if (stillRedBalls)
            {
                player2WasActiveWhenWon = (false == player2Active);
                SetPenaltyText("Potted the black without potting all your own balls first. You lose!");
            }
            else
            {
                player2WasActiveWhenWon = (player2Active);
            }
            GameOver();
        }
        Redraw();
    }

    void GameOver()
    {
        gameOver = true;
        gameOverText.text = player2WasActiveWhenWon ? "PLAYER\n2\nWINS!" : "PLAYER\n1\nWINS!";
        Redraw();

    }

    void Redraw()
    {
        player1ChevronImage.enabled = (!player2Active && !gameOver);
        player2ChevronImage.enabled = (player2Active && !gameOver);

        player1TwoShotsText.enabled = (twoShots && !player2Active && !gameOver);
        player2TwoShotsText.enabled = (twoShots && player2Active && !gameOver);
    }

    public void BallHalted()
    {
        Debug.Log("Ball halted");
        if (twoShots)
        {
            twoShots = false;
        }
        else
        {
            player2Active = !player2Active;
        }
        if (futureTwoShots)
        {
            futureTwoShots = false;
            twoShots = true;
        }
        Redraw();
    }

    public void ShotShotted()
    {
        Debug.Log("Shot shotted");
        Redraw();
    }

    public void AddPenalty()
    {
        Debug.Log("Penalty");
        twoShots = false;
        futureTwoShots = true;
    }

    public void SetPenaltyText(string text)
    {
        penaltyDescriptionText.text = text;
    }

    public BallColour.Colour GetUnfriendlyColour()
    {
        if (player2Active)
            return BallColour.Colour.Red;
        else
            return BallColour.Colour.Yellow;
    }

    public BallColour.Colour GetFriendlyColour()
    {
        if (player2Active)
            return BallColour.Colour.Yellow;
        else
            return BallColour.Colour.Red;
    }
}
